package com.example.xianji.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.activity.Main_My_CollectionActivity;
import com.example.xianji.R;
import com.example.xianji.bean.Collection;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Collection_Adapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Collection> list;
//    private SharedPreferences sp;
//    private Editor ed;

    public Collection_Adapter(Context mContext, ArrayList<Collection> list) {
        super();
        this.mContext = mContext;
        this.list = list;

//        sp = mContext.getSharedPreferences("text", mContext.MODE_PRIVATE);
//        ed = sp.edit();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;

        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(mContext, R.layout.collection_listview_item, null);

            mHolder.collection_title = (TextView) convertView.findViewById(R.id.collection_title);
            mHolder.collection_time = (TextView) convertView.findViewById(R.id.collection_time);
            mHolder.button_delect = (Button) convertView.findViewById(R.id.button_delect);
            convertView.setTag(mHolder);

        }
        mHolder = (Holder) convertView.getTag();
        mHolder.collection_title.setText(list.get(position).getTitle());
        mHolder.collection_time.setText(list.get(position).getCreated_at().substring(0, 10));

        final int a = position;
        if (Main_My_CollectionActivity.bianji_flag) {
            mHolder.button_delect.setVisibility(View.VISIBLE);

            mHolder.button_delect.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    delect(list.get(a).getId(), a);
                }
            });

        } else {
            mHolder.button_delect.setVisibility(View.GONE);

        }
		
		
		
		
		/*convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(list.get(a).getType().equals("demand")){
				
				Intent intent=new Intent(mContext,Main_XuQiu_DetailsActivity.class);
				intent.putExtra("xuqiu_id",list.get(a).getFid()+"");
				
				
				
				mContext.startActivity(intent);
				
				}else if(list.get(a).getType().equals("detail")){
					Intent intent=new Intent(mContext,Main_FuWu_DetailsActivity.class);
					intent.putExtra("fuwu_details_id",list.get(a).getFid());
					
					
					
					mContext.startActivity(intent);
					
				}
			}
		});*/
        return convertView;
    }

    public void delect(int fid, final int a) {

        HashMap<String, Object> map = new HashMap<String, Object>();


        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("fid", fid);
        Log.i("fid fid fid fid", fid + "");

        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.delect_collection,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(mContext, response.getString("msg"), Toast.LENGTH_SHORT).show();

                    if (response.getInt("status") == 1) {

                        list.remove(a);
                        Collection_Adapter.this.notifyDataSetChanged();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
				/*
					if(mCustomProgressDialog!=null){
						mCustomProgressDialog.dismiss();

						mCustomProgressDialog=null;
					}*/
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();


    }

    class Holder {
        TextView collection_title;
        TextView collection_time;
        Button button_delect;
    }

}
